package com.mopoba.app;

import java.util.Random;

import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.os.Handler;
import android.os.Handler.Callback;
import android.os.Message;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.Window;
import android.widget.TextView;

import com.mrpoid.shequ2.R;


/**
 * 
 * @author Yichou 2013-9-19
 *
 */
public class BaHomeActivity extends FragmentActivity 
	implements OnPageChangeListener, 
		Callback, 
		SurfaceHolder.Callback {
	TextView mTvNum;
	ViewPager mPager;
	Window mWindow;
	int mCurColor, mNextColor;
	float dr, dg, db;
	Handler mHandler;
	SurfaceView mSurfaceView;

	
	@Override
	public boolean handleMessage(Message msg) {
		switch(msg.what) {
		case 1: {
//			mBgDrawable.setColor(mNextColor);
//			mWindow.setBackgroundDrawable(mBgDrawable);
//			fadebg();
			mWindow.setBackgroundDrawable(new ColorDrawable(mNextColor));
			break;
		}
		
		default:
			return false;
		}

		return true;
	}
	
	private void initBg() {
		mWindow = getWindow();
		mWindow.setBackgroundDrawable(new ColorDrawable(0xffffe4c4));
		
		mCurColor = getResources().getColor(R.color.whitesmoke);
		mNextColor = getResources().getColor(R.color.sienna);
		
		dr = (Color.red(mNextColor) - Color.red(mCurColor))/3000f;
		dg = (Color.green(mNextColor) - Color.green(mCurColor))/3000f;
		db = (Color.blue(mNextColor) - Color.blue(mCurColor))/3000f;
	}
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_bahome);
		
		mHandler = new Handler(this);
		initBg();
		
		mTvNum = (TextView) findViewById(R.id.textView1);
		
		mPager = (ViewPager) findViewById(R.id.pager);
		mPager.setAdapter(new MyPagerAdapter(getSupportFragmentManager()));
		mPager.setOnPageChangeListener(this);
		
		mSurfaceView = (SurfaceView) findViewById(R.id.surfaceView1);
		mSurfaceView.getHolder().addCallback(this);
	}
	
	public void setBackgroundColor(int color) {
		mNextColor = 0xff000000 | color;
//		mHandler.sendEmptyMessageDelayed(1, 1000);
//		fadebg();
		mWindow.setBackgroundDrawable(new ColorDrawable(mNextColor));
	}
	
	protected BaHomeActivity getActivity() {
		return this;
	}
	
	public static class MyPagerAdapter extends FragmentPagerAdapter {

		public MyPagerAdapter(FragmentManager fm) {
			super(fm);
		}

		@Override
		public int getCount() {
			return 3;
		}

		@Override
		public Fragment getItem(int arg0) {
			BaseGridFragment f = null;

			if(arg0 == 0) {
				f = new PluginsGridFragment();
			} else {
				f = new OnlineGridFragment();
			}
			
			Random r = new Random();
			
			Bundle args = new Bundle();
			args.putInt("itemCount", r.nextInt(100));
			f.setArguments(args);
			
			return f;
		}
	}
	
	@Override
	public void onPageScrollStateChanged(int arg0) {
	}

	@Override
	public void onPageScrolled(int arg0, float arg1, int arg2) {
	}

	@Override
	public void onPageSelected(int arg0) {
		mTvNum.setText("" + (arg0+1) + "/3");
	}
	
	SurfaceHolder mHolder;
	boolean running = false;
	
	private void fadebg() {
		if(running || mHolder == null)
			return;
		
		running = true;
		System.out.println("src=" + Integer.toHexString(mCurColor) + ", dst=" + Integer.toHexString(mNextColor));
		
		new Thread(new Runnable() {
			
			@Override
			public void run() {
				try {
					Thread.sleep(500);
				} catch (InterruptedException e1) {
				}

				int sr = (mCurColor>>16) & 0xff, sg = (mCurColor>>8) & 0xff, sb = mCurColor&0xff;
				int dr = (mNextColor>>16) & 0xff, dg = (mNextColor>>8) & 0xff, db = mNextColor&0xff;
				
				long t0 = System.currentTimeMillis();
				while(true) {
					int d = (int) (System.currentTimeMillis() - t0);
					
					float f = d/1500.0f;
					int r = Math.round(sr + (dr - sr) * f);
					int g = Math.round(sg + (dg - sg) * f);
					int b = Math.round(sb + (db - sb) * f);
					
					if(r>dr) r = dr;
					if(g>dg) g = dg;
					if(b>db) b = db;
					
					Canvas c = mHolder.lockCanvas();

					int color = (0xff000000 | (r<<16) | (g<<8) | b);
//					System.out.println("color = " + Integer.toHexString(color));
					c.drawColor(color);
					
					mHolder.unlockCanvasAndPost(c);
					
					if(f > 1.5){
						mCurColor = mNextColor;
						running = false;
						System.out.println("end");
						break;
					}
					
					try {
						Thread.sleep(1000/30);
					} catch (InterruptedException e) {
					}
				}
			}
		}).start();
	}

	@Override
	public void surfaceCreated(SurfaceHolder holder) {
		mHolder = holder;
		
		System.out.println("++++++");
		
		if(mCurColor != mNextColor)
			fadebg();
		
		Canvas c = holder.lockCanvas();
		c.drawColor(mCurColor);
		holder.unlockCanvasAndPost(c);
	}

	@Override
	public void surfaceChanged(SurfaceHolder holder, int format, int width,
			int height) {
	}

	@Override
	public void surfaceDestroyed(SurfaceHolder holder) {
//		mHolder = null;
		System.out.println("-----");
	}
}
